package com.ruoyi.buss.excelnfo.controller;

import com.ruoyi.buss.excelnfo.service.ExprotToExcelService;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.domain.UserTaskRelationDO;
import com.ruoyi.buss.utils.ImportExcel;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/importExcel")
public class ImportExcelController {
    @Autowired
    private ExprotToExcelService exprotToExcelService;

    @Autowired
    private TokenService tokenService;

    @PreAuthorize(hasPermi = "buss:exportExcel:importTaskInfoExces")
    @Log(title = "任务客户名单导出", businessType = BusinessType.EXPORT)
    @PostMapping("/importTaskInfoExces")
    public AjaxResult importTaskInfoExces(@RequestParam(value = "file") MultipartFile file,
                                          @RequestParam(value = "taskName") String taskName,
                                          @RequestParam(value = "beginDate") String beginDate, @RequestParam(value = "endDate") String endDate) throws IOException {
        LoginUser userInfo = tokenService.getLoginUser();
        Boolean bool = ImportExcel.checkFile(file);
        if(!bool){
            throw new CustomException("文件异常!");
        }
    	try {
    		ExcelUtil<DataUserTaskRelation> util = new ExcelUtil<DataUserTaskRelation>(DataUserTaskRelation.class);
			List<DataUserTaskRelation> userList = util.importExcel(file.getInputStream());
            return exprotToExcelService.importTaskInfoExces(file,taskName,userInfo,beginDate,endDate,userList);
		} catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
    }


    @PreAuthorize(hasPermi = "buss:exportExcel:importUserTaskExcel")
    @Log(title = "营销任务分配导入", businessType = BusinessType.EXPORT)
    @PostMapping("/importUserTaskExcel")
    public AjaxResult importUserTaskExcel(@RequestParam(value = "file") MultipartFile file) throws IOException {
        LoginUser userInfo = tokenService.getLoginUser();
        Boolean bool = ImportExcel.checkFile(file);
        if(!bool){
            throw new CustomException("文件异常!");
        }
        try {
            ExcelUtil<UserTaskRelationDO> util = new ExcelUtil<UserTaskRelationDO>(UserTaskRelationDO.class);
            List<UserTaskRelationDO> userList = util.importExcel(file.getInputStream());
            return exprotToExcelService.importUserTaskExcel(file,userInfo,userList);
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
    }
}
